define(()=>{
    return function(res, productMsg, ele) {
        'use strict';
        let str = "";
        res.forEach(val=>{
            productMsg.forEach((val2,idx)=>{
                if(val.proId === val2.id){
                    str += `<tr class="item_tr">
                                <td class="check_td" index="${val2.id}" a="${idx}">
                                    <input type="checkbox" ${val2.flag ? 'checked':''} autocomplete="none" class="inp_check">
                                </td>
                                <td class="left_img">
                                    <div>
                                        <a href="./ItemDetails.html">
                                            <img src="${val.url}" alt="">
                                        </a>
                                    </div>
                                </td>
                                <td class="left_text">
                                    <div>
                                        <p>
                                            <a href="./ItemDetails.html">${val.name}</a>
                                        </p>
                                    </div>
                                </td>
                                <td class="unit_price">
                                    <div>￥ ${val.price}</div>
                                </td>
                                <td class="quantity" index="${val2.id}">
                                    <span class="Less fl">-</span>
                                    <span class="num fl">${val2.num}</span>
                                    <span class="push fl">+</span>
                                </td>
                                <td class="subtotal">
                                    <div>￥${parseInt(val.price * val2.num)}</div>
                                </td>
                                <td class="operating"  index="${val2.id}">
                                    <a class="del">删除</a>
                                    <br>
                                    <a class="favorites">移到我的收藏</a>
                                </td>
                            </tr>`;
                }
            })
        })
        ele.innerHTML = str;
        // 修改数量
        class Quantity{
            constructor(ops){
                this.les = ops.les;
                this.num = ops.num;
                this.pus = ops.pus;
                this.qny = ops.qny;
                this.stl = ops.stl;
                this.tl_ct = ops.tl_ct;
                this.tl_pc = ops.tl_pc;
                this.addEvent();
            }
            addEvent(){
                let _this = this;
                for(let i=0;i<this.les.length;i++){
                    this.les[i].addEventListener("click",function(eve){
                        let e = eve || window.event;
                        e.preventDefault();
                        if(_this.num[i].innerHTML === "1"){
                            _this.num[i].innerHTML = 1;
                        }else{
                            _this.num[i].innerHTML--;
                        }
                        // 数量存储
                        const id = _this.qny[i].getAttribute("index");
                        let l = 0;
                        productMsg.some((val,idx)=>{
                            l = idx;
                            return val.id === id;
                        })
                        productMsg[l].num = _this.num[i].innerHTML;
                        _this.stl[i].innerHTML = "￥"+(productMsg[l].num-0) * parseInt(productMsg[l].price-0)
                        localStorage.setItem("productMsg",JSON.stringify(productMsg));
                        // 修改数量
                        let sum = 0;
                        for(let n=0;n<productMsg.length;n++){
                            if(productMsg[n].flag){
                                sum += productMsg[n].num-0;
                            }
                        }
                        _this.tl_ct.innerHTML = sum;
                        // 计算总价
                        const allP = productMsg.reduce((start,val)=>val.flag ? start+= parseInt(val.num * val.price) : start, 0);
                        _this.tl_pc.innerHTML = allP;
                    })
                }
                for(let j=0;j<this.pus.length;j++){
                    this.pus[j].addEventListener("click",function(eve){
                        let nm = Number(_this.num[j].innerHTML)
                        let e = eve || window.event;
                        e.preventDefault();
                        nm++
                        _this.num[j].innerHTML = nm;
                        // 数量存储
                        const id = _this.qny[j].getAttribute("index");
                        let l = 0;
                        productMsg.some((val,idx)=>{
                            l = idx;
                            return val.id === id;
                        })
                        productMsg[l].num = _this.num[j].innerHTML;
                        _this.stl[j].innerHTML = "￥"+(productMsg[l].num-0) * parseInt(productMsg[l].price-0)
                        localStorage.setItem("productMsg",JSON.stringify(productMsg))
                        // 修改数量
                        let sum = 0;
                        for(let i=0;i<productMsg.length;i++){
                            if(productMsg[i].flag){
                                sum += productMsg[i].num-0
                            }
                        }
                        _this.tl_ct.innerHTML = sum;
                        // 计算总价
                        const allP = productMsg.reduce((start,val)=>val.flag ? start+= parseInt(val.num * val.price) : start, 0);
                        _this.tl_pc.innerHTML = allP;
                    })
                }
            }
        }
        new Quantity({
            les:document.querySelectorAll(".Less"),
            num:document.querySelectorAll(".num"),
            pus:document.querySelectorAll(".push"),
            qny:document.querySelectorAll(".quantity"),
            stl:document.querySelectorAll(".subtotal div"),
            tl_ct:document.querySelector(".total_count"),
            tl_pc:document.querySelector(".total_price")
        })
        class Del{
            constructor(ops){
                this.del = ops.del;
                this.inp_ck = ops.inp_ck;
                this.allFlag = ops.allFlag;
                this.tl_ct = ops.tl_ct;
                this.tl_pc = ops.tl_pc;
                this.b_del = ops.b_del;
                this.delAddEvent();
                this.inp_ckAddEvent();
            }
            // 删除
            delAddEvent(){
                for(let i=0;i<this.del.length;i++){
                    // 批量删除   有点bug未解决
                    let x = 0;
                    for(let j=0;j<productMsg.length;j++){
                        if(this.inp_ck[j].checked){
                            this.b_del.addEventListener("click",()=>{
                                let d = 0;
                                productMsg.forEach((val,idx)=>{
                                    d = idx;
                                    this.del[d].parentNode.parentNode.remove();
                                    if(val.flag){
                                        productMsg.splice(d, 1);
                                    }
                                })
                                localStorage.setItem("productMsg",JSON.stringify(productMsg));
                            })
                        }
                    }

                       
                    this.del[i].addEventListener("click",()=>{
                        this.del[i].parentNode.parentNode.remove();
                        const id = this.del[i].parentNode.getAttribute("index");
                        let j = 0;
                        productMsg.some((val,idx)=>{
                            j = idx;
                            return val.id === id;
                        })
                        productMsg.splice(j,1);
                        localStorage.setItem("productMsg",JSON.stringify(productMsg));
                        // 修改数量
                        let sum = 0;
                        for(let n=0;n<productMsg.length;n++){
                            if(productMsg[n].flag){
                                sum += productMsg[n].num-0
                            }
                        }
                        this.tl_ct.innerHTML = sum;
                        // 计算总价
                        const allP = productMsg.reduce((start,val)=>val.flag ? start+= parseInt(val.num * val.price) : start, 0);
                        this.tl_pc.innerHTML = allP;
                    })
                }
            }
            inp_ckAddEvent(){
            // 复选框
                for(let i=0;i<this.inp_ck.length;i++){
                    this.inp_ck[i].addEventListener("input",()=>{
                        const id = this.inp_ck[i].parentNode.getAttribute("index");
                        let j = 0;
                        productMsg.some((val,idx)=>{
                            j = idx;
                            return val.id === id;
                        })
                        productMsg[j].flag = Number(this.inp_ck[i].checked);
                        // console.log(Number(this.inp_ck[i].checked))
                        localStorage.setItem("productMsg",JSON.stringify(productMsg));
                        this.allFlag.checked = productMsg.every(val=>val.flag);
                        // 修改数量
                        let sum = 0;
                        for(let n=0;n<productMsg.length;n++){
                            if(productMsg[n].flag){
                                sum += productMsg[n].num-0
                            }
                        }
                        this.tl_ct.innerHTML = sum;
                        // 计算总价
                        const allP = productMsg.reduce((start,val)=>val.flag ? start+= parseInt(val.num * val.price) : start, 0);
                        this.tl_pc.innerHTML = allP;
                    })
                }
            }
        }
        new Del({
            del:document.querySelectorAll(".del"),
            inp_ck:document.querySelectorAll(".inp_check"),
            allFlag:document.querySelector(".allFlag"),
            tl_ct:document.querySelector(".total_count"),
            tl_pc:document.querySelector(".total_price"),
            b_del:document.querySelector(".batch_del")

        })
    }
});